package MyFitnessPlate.Handler;

import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import MyFitnessPlate.DataSet.ParsedExerciseDataSet;

public class ExerciseHandler extends DefaultHandler {
	/*
	 * <user> <id>{id}</id> <username>{username}</username>
	 * <firstname>{firstname}</firstname> <lastname>{lastname}</lastname>
	 * <email>{email}</email> <gender>{gender 1 if male, 0 if female}</gender>
	 * <weight>{weight in pounds}</weight> <height>{height in inches}</height>
	 * <age>{age}</age> </user>
	 */
	// ===========================================================
	// Fields
	// ===========================================================
	@SuppressWarnings("unused")
	private boolean in_xml = false;
	private boolean in_entry = false;
	private boolean in_id = false;
	private boolean in_duration = false;
	private boolean in_category = false;
	private boolean in_category_name = false;
	private boolean in_description = false;
	private boolean in_met = false;
	//private boolean in_description_text = false;
	
	
	
	private ArrayList<ParsedExerciseDataSet> myParsedExerciseDataSet;// = new ArrayList<ParsedFoodDataSet>(0);

	// ===========================================================
	// Getter & Setter
	// ===========================================================
	public ArrayList<ParsedExerciseDataSet> getParsedData() {
		return this.myParsedExerciseDataSet;
	}
	// ===========================================================
	public void startDocument() throws SAXException {
		this.myParsedExerciseDataSet = new ArrayList<ParsedExerciseDataSet>();
	}

	
	public void endDocument() throws SAXException {
		// Nothing to do
	}

	/**
	 * Gets be called on opening tags like: <tag> Can provide attribute(s), when
	 * xml was like: <tag attribute="attributeValue">
	 */
	
	public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
		if (localName.equals("xml")) {
			this.in_xml = true;
		} else if (localName.equals("entry")) {
			this.in_entry = true;
			myParsedExerciseDataSet.add(new ParsedExerciseDataSet());
		} else if (localName.equals("id")) {
			this.in_id = true;
		} else if (localName.equals("duration")) {
			this.in_duration = true;
		} else if (localName.equals("category")) {
			this.in_category = true;
		} else if (localName.equals("name")) {
			this.in_category_name = true;
		} else if (localName.equals("description")) {
			this.in_description = true;			
		} else if (localName.equals("met")) {
			this.in_met = true;
		} 
	}

	/**
	 * Gets be called on closing tags like: </tag>
	 */
	
	public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
		if (localName.equals("xml")) {
			this.in_xml = false;
		} else if (localName.equals("entry")) {
			this.in_entry = false;
		} else if (localName.equals("id")) {
			this.in_id = false;
		} else if (localName.equals("duration")) {
			this.in_duration = false;
		} else if (localName.equals("category")) {
			this.in_category = false;
		} else if (localName.equals("name")) {
			this.in_category_name = false;
		} else if (localName.equals("description")) {
			this.in_description = false;			
		} else if (localName.equals("met")) {
			this.in_met = false;
		} 
	}

	/**
	 * Gets be called on the following structure: <tag>characters</tag>
	 */
	
	public void characters(char ch[], int start, int length) {
		String characterString = new String(ch, start, length);
		if (this.in_entry && this.in_id && !this.in_category && !this.in_description) {
			
			myParsedExerciseDataSet.get(myParsedExerciseDataSet.size()-1).setEntry_id(characterString.trim());
		}
		if (this.in_duration) {
			myParsedExerciseDataSet.get(myParsedExerciseDataSet.size()-1).setDuration(characterString);
		}
		if (this.in_category && this.in_id) {
			myParsedExerciseDataSet.get(myParsedExerciseDataSet.size()-1).setCategoryId(characterString);
		}
		if (this.in_category_name) {
			myParsedExerciseDataSet.get(myParsedExerciseDataSet.size()-1).setCategoryName(characterString);
		}
		if (this.in_description && this.in_id) {
			myParsedExerciseDataSet.get(myParsedExerciseDataSet.size()-1).setDescriptionId(characterString);
		}
		if (this.in_met) {
			myParsedExerciseDataSet.get(myParsedExerciseDataSet.size()-1).setMet(characterString);
		}
		if (this.in_description) {
			myParsedExerciseDataSet.get(myParsedExerciseDataSet.size()-1).setDescriptionName(characterString);
		}
	}
}
